﻿using Hospital.POCOModel;
using Hospital.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace Hospital.Controllers
{
    [Authorize]
    public class HomeController : Controller
    {
        static string UName = "";
        public ActionResult Index()
        {
            return View();
        }
        [AllowAnonymous]
        public ActionResult Login()
        {
            return View();
        }
        [AllowAnonymous]
        public ActionResult UserLogin(string name, string pwd)
        {
            using (var db = new HospitalEntity())
            {
                var user = db.AdminUser.FirstOrDefault(u => u.Name == name && u.Password == pwd);
                UName = user.Name;
                if (user == null)
                {
                    return Json(new { State = -1, Message = "登录失败" });
                }
                else
                {
                    FormsAuthentication.SetAuthCookie(name, false);
                    return Json(new { State = 1, Message = "登录成功" });
                }
            }
        }
        public ActionResult Default(string Name = "", string Category = "", string Department = "")
        {
            using (var db = new HospitalEntity())
            {
                var list = db.Specialist.Where(s => (s.Name.Contains(Name)) && (s.Category.Contains(Category)) && (s.Department.Contains(Department))).Select(s => new SpecialistViewModel()
                {
                    ID = s.ID,
                    Name = s.Name,
                    Sex = s.Sex,
                    Category = s.Category,
                    Department = s.Department,
                    TitleName = s.TitleName,
                    GetDateTime = DateTime.Now
                }).ToList();
                return View(list);
            }
        }
        [HttpGet]
        public ActionResult Add()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Add(SpecialistAddViewModel model)
        {
            if (ModelState.IsValid)
            {
                using (var db = new HospitalEntity())
                {
                    var spe = new Specialist()
                    {
                        Name = model.Name,
                        Sex = model.Sex,
                        Specialty = model.Specialty,
                        Avatar = model.Avatar,
                        Category = model.Category,
                        CreateDateTime = DateTime.Now,
                        TitleName = model.TitleName,
                        Department = model.Department,
                    };
                    db.Specialist.Add(spe);
                    db.SaveChanges();
                    ViewBag.msg = "添加成功";
                }
            }
            else
            {
                ModelState.AddModelError("", "添加失败");
            }
            return View();
        }
        [HttpGet]
        public ActionResult Edit(int id)
        {
            using (var db = new HospitalEntity())
            {
                var res = db.Specialist.FirstOrDefault(s => s.ID == id);
                SpecialistEditViewModel sp = new SpecialistEditViewModel()
                {
                    ID = res.ID,
                    Name = res.Name,
                    Sex = res.Sex,
                    Avatar = res.Avatar,
                    Category = res.Category,
                    Department = res.Department,
                    GetDateTime = res.CreateDateTime,
                    TitleName = res.TitleName,
                    Specialty = res.Specialty
                };
                return View(sp);
            }
        }
        [HttpPost]
        public ActionResult Edit(SpecialistEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                using (var db = new HospitalEntity())
                {
                    var spe = new Specialist()
                    {
                        Name = model.Name,
                        Sex = model.Sex,
                        Specialty = model.Specialty,
                        Avatar = model.Avatar,
                        Category = model.Category,
                        CreateDateTime = DateTime.Now,
                        TitleName = model.TitleName,
                        Department = model.Department,
                    };
                    db.Specialist.Add(spe);
                    db.SaveChanges();
                    ViewBag.msg = "修改成功";
                }
            }
            else
            {
                ModelState.AddModelError("", "修改失败");
            }
            return View();
        }
        public ActionResult Delete(int id)
        {
            using (var db = new HospitalEntity())
            {
                var user = db.Specialist.FirstOrDefault(s => s.ID == id);
                if (user == null)
                {
                    return Json(new { State = -1, Message = "删除失败" });
                }
                db.Specialist.Remove(user);
                db.SaveChanges();
                return Json(new { State = 1, Message = "删除成功" });
            }
        }
    }
}